/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int en;
    TreeNode* build(vector<int>& inorder, vector<int>& postorder,int inLeft,int inRight)
    {
        if(en<0 || inLeft>inRight)
            return nullptr;
        TreeNode* u=new TreeNode(postorder[en--]);
        int mid=0;
        for(;mid<inorder.size();mid++)
        {
            if(inorder[mid]==u->val)
                break;
        }
        u->right=build(inorder,postorder,mid+1,inRight);
        u->left=build(inorder,postorder,inLeft,mid-1);
        return u;
    }
    TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
        en=postorder.size()-1;
        return build(inorder,postorder,0,inorder.size()-1);
    }
};
